//
//  lightView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/27.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "lightView.h"

@interface lightView ()<UIScrollViewDelegate>
{
    //__weak UISlider *mySlide;
}

@property (nonatomic, strong) UIScrollView  *scrollView;

@end

@implementation lightView

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addLightView
{
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/back_home_dim");
    UIImageView *backView = [[UIImageView alloc] initWithImage:image];
    [backView setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height / 3)];
    [self.scrollView addSubview:backView];
    UIImageView *airView = [[UIImageView alloc] initWithImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/light/light")];
    [airView setFrame:CGRectMake(0, 0, backView.bounds.size.width / 2, backView.bounds.size.height)];
    CGFloat h = backView.bounds.size.height - airView.bounds.size.height / 2;
    airView.center = CGPointMake(backView.bounds.size.width / 2, h);
    [backView addSubview:airView];
}

- (void)addUsualLight
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 3, self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/fundationView/light/light_on");
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:image];
    [leftImageView setFrame:CGRectMake(0, 0, 50, 50)];
    leftImageView.center = CGPointMake(35, view.bounds.size.height / 2);
    [view addSubview:leftImageView];
    leftImageView = nil;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, view.bounds.size.height / 2 - 10, 60, 20)];
    label.text = @"普通灯";
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [view addSubview:label];
    label = nil;
    
    UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(view.bounds.size.width - 150, view.bounds.size.height / 2 - 25, 50, 50)];
    [closeButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(pressClose) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:closeButton];
    closeButton = nil;
    
    UIButton *openButton = [[UIButton alloc] initWithFrame:CGRectMake(view.bounds.size.width - 70, view.bounds.size.height / 2 - 25, 50, 50)];
    [openButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
    [openButton addTarget:self action:@selector(pressOpen) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:openButton];
    openButton = nil;
    
    [self.scrollView addSubview:view];
    view = nil;
}

- (void)addOtherLight
{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 5 + 2 + self.bounds.size.height / 3,
                                                            self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImage *image = GET_IAMGE_WITH_NAME(@"Image/fundationView/light/light_on");
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:image];
    [leftImageView setFrame:CGRectMake(0, 0, 50, 50)];
    leftImageView.center = CGPointMake(35, view.bounds.size.height / 2);
    [view addSubview:leftImageView];
    leftImageView = nil;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, view.bounds.size.height / 2 - 10, 60, 20)];
    label.text = @"调光灯";
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [view addSubview:label];
    label = nil;
    
    [self.scrollView addSubview:view];
    view = nil;
    
    UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 5 * 2 + 4 + self.bounds.size.height / 3,
                                                                 self.bounds.size.width, 80)];
    otherView.backgroundColor = [UIColor whiteColor];
    
    UIImage *leftImage = GET_IAMGE_WITH_NAME(@"Image/fundationView/light/light_off");
    UIImageView *leftView = [[UIImageView alloc] initWithImage:leftImage];
    UIImageView *rightView = [[UIImageView alloc] initWithImage:image];
    [leftView setFrame:CGRectMake(10, otherView.bounds.size.height / 2 - 20, 40, 40)];
    [rightView setFrame:CGRectMake(otherView.bounds.size.width - 50, otherView.bounds.size.height / 2 - 20, 40, 40)];
    [otherView addSubview:leftView];
    [otherView addSubview:rightView];
    leftView = nil;
    rightView = nil;
    
    UISlider *slide = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, otherView.bounds.size.width - 120, 35)];
    slide.center = CGPointMake(otherView.bounds.size.width / 2, otherView.bounds.size.height / 2);
    [otherView addSubview:slide];
    slide.maximumValue = 4.0;
    slide.minimumValue = 0.0;
    slide.minimumTrackTintColor = TEXT_GREEN_COLOR;
    [slide addTarget:self action:@selector(slideValueChange) forControlEvents:UIControlEventValueChanged];
    [slide addTarget:self action:@selector(touchSlider) forControlEvents:UIControlEventTouchUpInside];
    slide.tag = 1;
    slide = nil;
    
    otherView.tag = 0;
    [self.scrollView addSubview:otherView];
    otherView = nil;
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, otherView.bounds.size.height + self.bounds.size.height / 5 * 2 + self.bounds.size.height / 3 + 4);
}

#pragma mark - 按钮响应函数
- (void)pressOpen
{
    //开灯
    [self.delegate sendRespondByFuctionName:PD_ON];
}

- (void)pressClose
{
    //关灯
    [self.delegate sendRespondByFuctionName:PD_OFF];
}

- (void)slideValueChange
{
    
}

- (void)touchSlider
{
    UISlider *slide = (UISlider *)[[self.scrollView viewWithTag:0] viewWithTag:1];
    if(slide.value >= 0 && slide.value < 0.5){
        slide.value = 0;
    }else if(slide.value >= 0.5 && slide.value < 1.5){
        slide.value = 1;
    }else if(slide.value >= 1.5 && slide.value < 2.5){
        slide.value = 2;
    }else if(slide.value >= 2.5 && slide.value < 3.5){
        slide.value = 3;
    }else if(slide.value >= 3.5 && slide.value < 4.5){
        slide.value = 4;
    }
    if(slide.value == 0){
        [self.delegate sendRespondByFuctionName:TD_VALUE0];
    }else if(slide.value == 1){
        [self.delegate sendRespondByFuctionName:TD_VALUE1];
    }else if(slide.value == 2){
        [self.delegate sendRespondByFuctionName:TD_VALUE2];
    }else if(slide.value == 3){
        [self.delegate sendRespondByFuctionName:TD_VALUE3];
    }else if(slide.value == 4){
        [self.delegate sendRespondByFuctionName:TD_VALUE4];
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addLightView];
        [self addUsualLight];
        [self addOtherLight];
    }
    
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
